<?php

namespace app\merchant\controller\product;
use app\common\controller\BaseController;
use app\common\service\product\ShopProductService;
use app\common\validate\product\ShopProductValidate;
use app\common\service\api\TaoceSdkService;
use hg\apidoc\annotation as Apidoc;
/**
 * @Apidoc\Title("商品管理")
 * @Apidoc\Group("product")
 * @Apidoc\Sort("250")
 */
class ShopProduct extends BaseController
{
    /**
     * @Apidoc\Title("商品管理列表")
     * @Apidoc\Query(ref="pagingQuery")
     * @Apidoc\Query(ref="sortQuery")
     * @Apidoc\Query(ref="searchQuery")
     * @Apidoc\Query(ref="dateQuery")
     * @Apidoc\Returned(ref="expsReturn")
     * @Apidoc\Returned(ref="pagingReturn")
     * @Apidoc\Returned("list", type="array", desc="商品管理列表", children={
     *   @Apidoc\Returned(ref="app\common\model\ShopProductModel", field="id,title,content,is_disable,create_time,update_time,store_platform_id,merchant_id,merchant_shop_id,setting_refund_id,shop_product_type_id,status,money,remark,b_is_delivery,b_delivery_price,b_coupon_url,b_coupon_price,c_sort_type,c_pay_num,c_start_price,c_end_price,c_region_id,c_order_remark,c_subject,d_enter_type,e_revoke_time,f_auth_type,f_reward_price,h_pay_time,h_pay_type,sort,order_num,f_increment_num,f_increment_price,d_comment_num,d_comment_price,b_product_num,b_product_price")
     * })
     */
    public function list()
    {
        $where = $this->buildWhere([
            'is_disable',
            'status',
            'platform_ids',
            'product_type_ids',
            'merchant_shop_id',
            'setting_refund_id'
        ]);
        $where = $this->where(where_delete($where));
        $where[] = ['merchant_id','=',mer_id()];
        $data = ShopProductService::list($where, $this->page(), $this->limit(), $this->order());
        return success($data);
    }

    /**
     * @Apidoc\Title("查询发布商品时所需参数")
     * @Apidoc\Query(ref="app\common\model\ShopProductModel", field="id")
     * @Apidoc\Returned(ref="app\common\model\ShopProductModel")
     */
    public function getListParams()
    {
        $data = ShopProductService::getListParams();
        return success($data);
    }

    /**
     * @Apidoc\Title("查询发布商品时所需参数")
     * @Apidoc\Query(ref="app\common\model\ShopProductModel", field="id")
     * @Apidoc\Returned(ref="app\common\model\ShopProductModel")
     */
    public function getParams()
    {
        $where = $this->where(where_delete(['is_disable','<>',1]));
        $data = ShopProductService::getParams($where,$this->order());
        return success($data);
    }
    /**
     * @Apidoc\Title("查询发布商品时所需参数")
     * @Apidoc\Query(ref="app\common\model\ShopProductModel", field="id")
     * @Apidoc\Returned(ref="app\common\model\ShopProductModel")
     */
    public function getParams1()
    {
        $where = $this->where(where_delete(['is_disable','<>',1]));
        $data = ShopProductService::getParams1($where,$this->order());
        return success($data);
    }

    /**
     * @Apidoc\Title("商品管理信息")
     * @Apidoc\Query(ref="app\common\model\ShopProductModel", field="id")
     * @Apidoc\Returned(ref="app\common\model\ShopProductModel")
     */
    public function info()
    {
        $param = $this->params(['id/d' => '']);
        validate(ShopProductValidate::class)->scene('info')->check($param);
        $data = ShopProductService::info($param['id']);
        return success($data);
    }
    /**
     * @Apidoc\Title("商品管理添加")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="app\common\model\ShopProductModel", field="title,content,is_disable,store_platform_id,merchant_id,merchant_shop_id,setting_refund_id,shop_product_type_id,status,money,remark,b_is_delivery,b_delivery_price,b_coupon_url,b_coupon_price,c_sort_type,c_pay_num,c_start_price,c_end_price,c_region_id,c_order_remark,c_subject,d_enter_type,e_revoke_time,f_auth_type,f_reward_price,h_pay_time,h_pay_type,sort,order_num,f_increment_num,f_increment_price,d_comment_num,d_comment_price,b_product_num,b_product_price")
     */
    public function add()
    {
        $fields = ShopProductService::$edit_field;
        $fields =array_merge($fields,[
            'product_goods/a'=>[],//商品信息
            'enter_shop_types/a'=>[],//进店方式
            'creat_order_plan/a'=>[],//放单计划
            'f_increment_ids/a'=>[],//增值id
            'f_increment_values'=>null,//增值选中值
            'remark_images/a'=>[],//备注图片
            'price_details/a'=>[],//价格明细
            'e_order_time/a'=>[],//立刻放单时间范围
            'release_order_num/d'=>0,//放单总数
        ]);
        $param = $this->params($fields);
        validate(ShopProductValidate::class)->scene('add')->check($param);
        $data = ShopProductService::add($param);
        return success($data);
    }
    /**
     * @Apidoc\Title("商品管理修改")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="app\common\model\ShopProductModel", field="id,title,content,is_disable,store_platform_id,merchant_id,merchant_shop_id,setting_refund_id,shop_product_type_id,status,money,remark,b_is_delivery,b_delivery_price,b_coupon_url,b_coupon_price,c_sort_type,c_pay_num,c_start_price,c_end_price,c_region_id,c_order_remark,c_subject,d_enter_type,e_revoke_time,f_auth_type,f_reward_price,h_pay_time,h_pay_type,sort,order_num,f_increment_num,f_increment_price,d_comment_num,d_comment_price,b_product_num,b_product_price")
     */
    public function edit()
    {
        $fields = ShopProductService::$edit_field;
        $fields =array_merge($fields,[
            'product_goods/a'=>[],//商品信息
            'enter_shop_types/a'=>[],//进店方式
            'creat_order_plan/a'=>[],//放单计划
            'f_increment_ids/a'=>[],//增值id
            'f_increment_values'=>null,//增值选中值
            'remark_images/a'=>[],//备注图片
            'price_details/a'=>[],//价格明细
            'e_order_time/a'=>[],//立刻放单时间范围
            'release_order_num/d'=>0,//放单总数
        ]);
        $param = $this->params($fields);
        validate(ShopProductValidate::class)->scene('edit')->check($param);;
        $data = ShopProductService::edit($param['id'], $param);
        return success($data);
    }
    /**
     * @Apidoc\Title("商品管理删除")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     */
    public function dele()
    {
        $param = $this->params(['ids/a' => []]);
        validate(ShopProductValidate::class)->scene('dele')->check($param);
        $data = ShopProductService::dele($param['ids']);
        return success($data);
    }
    /**
     * @Apidoc\Title("商品管理禁用")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\ShopProductModel", field="is_disable")
     */
    public function disable()
    {
        $param = $this->params(['ids/a' => [], 'is_disable/d' => 0]);
        validate(ShopProductValidate::class)->scene('disable')->check($param);
        $data = ShopProductService::edit($param['ids'], $param);
        return success($data);
    }
    /**
     * @Apidoc\Title("商品撤销")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     */
    public function revoke()
    {
        $param = $this->params(['ids/a' => [],'revoke_remark/s'=>""]);
        validate(ShopProductValidate::class)->scene('revoke')->check($param);
        $data = ShopProductService::revoke($param['ids'],$param);
        return success($data);
    }
    /**
     * @Apidoc\Title("商品支付")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     */
    public function product_pay()
    {
        $param = $this->params(['ids/a' => []]);
        validate(ShopProductValidate::class)->scene('pay')->check($param);
        $data = ShopProductService::pay($param['ids']);
        return success($data);
    }
    /**
     * @Apidoc\Title("商品暂停")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     */
    public function product_halt()
    {
        $param = $this->params(['ids/a' => []]);
        validate(ShopProductValidate::class)->scene('halt')->check($param);
        $data = ShopProductService::halt($param['ids']);
        return success($data);
    }
    /**
     * @Apidoc\Title("商品开启")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     */
    public function product_open()
    {
        $param = $this->params(['ids/a' => []]);
        validate(ShopProductValidate::class)->scene('open')->check($param);
        $data = ShopProductService::open($param['ids']);
        return success($data);
    }





    public function information()
    {
        //type 1:淘宝 2:京东 3:拼多多 4:抖音 5:天猫 6:阿里巴巴
        $param = $this->params(['type' => "",'parsed_url' => ""]);
        return success(TaoceSdkService::information($param));
    }

}
